package com.tengju.user.application.login;

import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.data.domain.model.wx.WxUserInfo;
import com.tengju.data.domain.model.wx.WxUserInfoRepository;
import com.tengju.support.domain.model.wx.UserSession;
import com.tengju.support.domain.service.WeiXinCompanyService;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.application.shared.ApplicationExceptionCode;
import com.tengju.user.domain.model.login.LoginForm;
import com.tengju.user.domain.model.login.LoginResult;
import com.tengju.user.domain.model.login.UserAccount;
import com.tengju.user.domain.model.login.UserType;
import com.tengju.user.domain.model.user.UserAccountRepository;
import com.tengju.user.domain.model.user.UserId;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.model.user.UserInfoRepository;
import com.tengju.user.domain.shared.IdObject;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @description: 企业微信小程序
 * @since: 2021/12/22 6:51 下午
 * @author: xiajichen
 */
@Service("qyMiniProgramLoginStrategyImpl")
public class QyMiniProgramLoginStrategyImpl implements LoginStrategy {

    private WeiXinCompanyService weiXinCompanyService;
    private WxUserInfoRepository wxUserInfoRepository;
    private UserInfoRepository userInfoRepository;
    private UserAccountRepository userAccountRepository;

    public QyMiniProgramLoginStrategyImpl(WeiXinCompanyService weiXinCompanyService,
                                          WxUserInfoRepository wxUserInfoRepository,
                                          UserInfoRepository userInfoRepository,
                                          UserAccountRepository userAccountRepository) {
        this.weiXinCompanyService = weiXinCompanyService;
        this.wxUserInfoRepository = wxUserInfoRepository;
        this.userInfoRepository = userInfoRepository;
        this.userAccountRepository = userAccountRepository;
    }

    @Override
    public LoginResult login(LoginForm loginForm, UserAccount userAccount) {
        UserSession userSession = null;
        try {
            userSession = weiXinCompanyService.codeToSession(loginForm.getAuthCode());
        } catch (Exception e) {
            throw ApplicationException.error(ApplicationExceptionCode.LOGIN_FAIL);
        }
        if (null == userSession || null == userSession.getIdCode()) {
            throw ApplicationException.error(ApplicationExceptionCode.LOGIN_FAIL);
        }
        WxUserInfo wxUserInfo = wxUserInfoRepository.selectByIdCode(UserIdCode.of(userSession.getIdCode()));

        if (null == wxUserInfo) {
            throw ApplicationException.error(ApplicationExceptionCode.USER_NOT_REGISTER);
        }
        // 查询是否存在服务商用户身份
        com.tengju.user.domain.model.user.UserIdCode userIdCode =
                com.tengju.user.domain.model.user.UserIdCode.of(wxUserInfo.getIdCode().getValue());
        UserInfo userInfo = userInfoRepository.getByIdCode(userIdCode);

        userAccount = userAccountRepository
                .queryUserAccount(loginForm.getUserType(), new IdObject(userInfo.getId()));
        if (null == userAccount) {
            throw ApplicationException.error(ApplicationExceptionCode.USER_NOT_REGISTER);
        }

        //更新上次登录ip及登录时间
        userAccount.login(loginForm.getLoginIp());
        userAccountRepository.login(userAccount);
        return new LoginResult(userAccount.getUserId().getValue(),
                userAccount.getUsername(),
               null,
               null,
                null,
                UserType.USER,
                userIdCode,
                new ArrayList<>(),
                new ArrayList<>(),
                userAccount.getPassword(),
                new ArrayList<>(),
        null == userInfo);
    }

    @Override
    public LoginResult registerAndLogin(LoginForm loginForm) {
        throw ApplicationException.error(ApplicationExceptionCode.LOGIN_FAIL, "暂不支持改登录方式");
    }
}
